﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Boiling.DataAccess
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Reflection;
    using System.Text;

    using Boiling.Common;

    public class b_UserAddInfo : IDisposable
    {
        private SqlDataAdapter dscommand;

        public b_UserAddInfo()
        {
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
                return;
            if (dscommand != null)
            {
                if (dscommand.SelectCommand != null)
                {
                    if (dscommand.SelectCommand.Connection != null)
                        dscommand.SelectCommand.Connection.Dispose();
                    dscommand.SelectCommand.Dispose();
                }
                dscommand.Dispose();
                dscommand = null;
            }
        }
        //
        //指令资源分配方式的代码段，与逻辑无关
        //

        public b_UserAddInfodata getuseraddinfobyID(string username)
        {
            //用用户注册邮箱来获取用户联络信息
            b_UserAddInfodata newdata = new b_UserAddInfodata();
            string selectstring = "SELECT * FROM b_UserAddInfo WHERE User_ID =" + "'" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();
            dscommand.Fill(newdata, "b_UserAddInfo");
            tempconn.Close();
            return (newdata);
        }

        public bool deleteuseraddinfo(string username)
        {
            //根据用户名删除对应的用户个人联络信息
            //如果用户名存在且删除成功，返回true
            //其他情况返回false
            b_UserAddInfodata newdata = new b_UserAddInfodata();
            string selectstring = "SELECT * FROM b_UserAddInfo WHERE User_ID =" + "'" + username + "'";
            dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
            SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
            SqlCommand selcmd = dscommand.SelectCommand;
            SqlConnection tempconn = selcmd.Connection;
            tempconn.Open();

            dscommand.Fill(newdata, "b_UserAddInfo");

            if (newdata.Tables["b_UserAddInfo"].Rows.Count == 0)
                return (false);
            else
            {
                newdata.Tables["b_UserAddInfo"].Rows[0].Delete();
                dscommand.Update(newdata, "b_UserAddInfo");
                tempconn.Close();
                return (true);
            }
        }
        public bool checkuseraddinfobyID(string username)
        {
            //根据用户名检察用户是否存在对应个人基本信息
            b_UserAddInfodata newdata = getuseraddinfobyID(username);
            if (newdata.Tables["b_UserAddInfo"].Rows.Count == 0)
                return false;
            else
                return true;
        }

        public bool modifyuseraddinfo(b_UserAddInfodata newdata)
        {
            //输入一个用户联络信息，修改对应用户注册邮箱处的个人联络信息为新信息
            string userID = newdata.Tables["b_UserAddInfo"].Rows[0][0].ToString();
            if (checkuseraddinfobyID(userID))
            {

                deleteuseraddinfo(userID);
                insertuseraddinfo(newdata);
                return true;
            }
            else
                return false;
        }

        public bool insertuseraddinfo(b_UserAddInfodata newdata)
        {
            //输入一个用户联络信息，插入到数据库中去
            if (newdata.Tables["b_UserAddInfo"].Rows.Count == 0)
                return false;
            else
            {
                string insertuserID = newdata.Tables["b_UserAddInfo"].Rows[0]["User_ID"].ToString();
                if (checkuseraddinfobyID(insertuserID))
                    return false;
                else
                {
                    string selectstring = "SELECT * FROM b_UserAddInfo";
                    b_UserAddInfodata testdata = new b_UserAddInfodata();
                    dscommand = new SqlDataAdapter(selectstring, BoilingConfiguration.dbConnnectionstring);
                    SqlCommandBuilder da = new SqlCommandBuilder(dscommand);
                    SqlCommand selcmd = dscommand.SelectCommand;
                    SqlConnection tempconn = selcmd.Connection;
                    tempconn.Open();
                    dscommand.Fill(testdata, "b_UserAddInfo");
                    DataRow newrow = testdata.Tables["b_UserAddInfo"].NewRow();
                    int i;
                    for (i = 0; i < 5; i++)
                    {
                        newrow[i] = newdata.Tables["b_UserAddInfo"].Rows[0][i];
                    }
                    testdata.Tables["b_UserAddInfo"].Rows.Add(newrow);
                    dscommand.Update(testdata, "b_UserAddInfo");
                    tempconn.Close();
                    return (true);
                }
            }
        }
    }
}
